using System;
using System.Data;
using System.Linq;
using Easy.Data;
using Easy.Data.Linq;
using Easy.Model;
using System.Collections.Generic;
using Easy.Common;

namespace Easy.Service.DataAccess
{
    /// <summary>
    /// 考勤数据访问层
    /// </summary>
    public class MediaDataAccess
    {
        /// <summary>
        /// 添加考勤数据
        /// </summary>
        public static void AddMedia(MediaModel model)
        {
            using (ISession session = SessionFactory.CreateDefaultSession())
            {
                var tran = session.BeginTransaction();
                try
                {
                    model.MediaId = ObjectId.GenerateNewId().ToString().ToUpper();
                    session.Delete(model);
                    int index = session.Insert(model);
                    
                    if (index < 1) throw new Exception("数据库受影响的行数小于1,添加失败");
                    
                    if (!model.MediaImage.IsNullOrEmpty())
                    {
                        model.MediaImage.MediaId = model.MediaId;
                        AddMediaImage(session, model.MediaImage);
                    }
                    tran.Commit();
                }
                catch (Exception ex)
                {
                    tran.Rollback();
                    throw ex;
                }
            }
        }

        /// <summary>
        /// 添加媒体图片
        /// </summary>
        public static void AddMediaImage(ISession session, MediaFileModel file)
        {
            file.FileId = ObjectId.GenerateNewId().ToString().ToUpper();
            file.FileSuffix = ".jpg";
            file.CreateTime = DateTime.Now;
            
            session.Delete(file);
            session.Delete<MediaFileModel>().Where(p => p.MediaId.Equals(file.MediaId)).Execute();
            
            int index = session.Insert(file);
            if (index < 1) throw new Exception("数据库受影响的行数小于1,添加失败");
        }

        /// <summary>
        /// 修改考勤数据
        /// </summary>
        public static void UpdateMedia(MediaModel model)
        {
            using (ISession session = SessionFactory.CreateDefaultSession())
            {
                var tran = session.BeginTransaction();
                try
                {
                    int index = session.Update(model, false);
                    if (index < 1) throw new Exception("数据库受影响的行数小于1,修改失败");
                    
                    if (!model.MediaImage.IsNullOrEmpty())
                    {
                        model.MediaImage.MediaId = model.MediaId;
                        AddMediaImage(session, model.MediaImage);
                    }
                    tran.Commit();
                }
                catch (Exception ex)
                {
                    tran.Rollback();
                    throw ex;
                }
            }
        }

        /// <summary>
        /// 查询考勤数据
        /// </summary>
        public static DataPage SearchMedia(SearchMediaModel model)
        {
            using (ISession session = SessionFactory.CreateDefaultSession())
            {
                var result = session.Find<ViewMediaModel>().DataPagingAndSorting(model);
                Code.CodeHelper.FillCode<ViewMediaModel>(result.data as List<ViewMediaModel>);
                return result;
            }
        }

        /// <summary>
        /// 根据ID获取考勤数据
        /// </summary>
        public static MediaModel GetMedia(string MediaId)
        {
            using (ISession session = SessionFactory.CreateDefaultSession())
            {
                MediaModel model = session.Find<MediaModel>().Where(p => p.MediaId.Equals(MediaId)).FirstOrDefault();
                Code.CodeHelper.FillCode<MediaModel>(model);
                return model;
            }
        }

        /// <summary>
        /// 根据ID获取视图考勤数据
        /// </summary>
        public static ViewMediaModel GetViewMedia(string MediaId)
        {
            using (ISession session = SessionFactory.CreateDefaultSession())
            {
                ViewMediaModel model = session.Find<ViewMediaModel>().Where(p => p.MediaId.Equals(MediaId)).FirstOrDefault();
                Code.CodeHelper.FillCode<ViewMediaModel>(model);
                return model;
            }
        }

        /// <summary>
        /// 获取公司考勤数据
        /// </summary>
        public static List<MediaModel> GetCompanyMedias(string companyId)
        {
            using (ISession session = SessionFactory.CreateDefaultSession())
            {
                List<MediaModel> models = session.Find<MediaModel>().Where(p => p.MediaCompanyId.Equals(companyId) && p.MediaApproveState.Equals("02")).ToList();
                Code.CodeHelper.FillCode<MediaModel>(models);
                return models;
            }
        }

        /// <summary>
        /// 获取媒体图片
        /// </summary>
        public static byte[] GetMediaImage(string MediaId)
        {
            using (ISession session = SessionFactory.CreateDefaultSession())
            {
                var bts = session.ExecuteScalar<byte[]>("Select FileContent from Info_Media_File Where MediaId=? ", MediaId);
                return bts;
            }
        }

        /// <summary>
        /// 获取时间段内的考勤数据
        /// </summary>
        public static List<ApiMediaModel> GetMedias(string region, DateTime beginTime, DateTime endTime)
        {
            using (ISession session = SessionFactory.CreateDefaultSession())
            {
                List<ApiMediaModel> models = session.Find<ApiMediaModel>().Where(p => p.MediaRegion.StartsWith(region) && p.MediaApproveState.Equals("02") && p.MediaApproveTime > beginTime && p.MediaApproveTime < endTime).ToList();
                Code.CodeHelper.FillCode<ApiMediaModel>(models);
                return models;
            }
        }

        /// <summary>
        /// 获取考勤列表
        /// </summary>
        public static List<MediaModel> GetMediaLists(string mediaIds)
        {
            using (ISession session = SessionFactory.CreateDefaultSession())
            {
                var medias = session.Find<MediaModel>(string.Format("Select MediaId,MediaCount,MediaPointEast,MediaPointNorth,MediaRegion From Info_Media where MediaApproveState='02' And  MediaId in ({0}) ", mediaIds)).ToList();
                Code.CodeHelper.FillCode<MediaModel>(medias);
                return medias;
            }
        }
    }
}